<?php
$city=$_POST['city'];
$date=$_POST['date'];
$lock=$_POST['lock'];
$stock=$_POST['stock'];
$barrel=$_POST['barrel'];
session_start();
$id=$_SESSION['userid'];
$name=$_SESSION['name'];

if ($lock === ""){
    $lock = 0;
}
if ($stock === ""){
    $stock = 0;
}
if ($barrel === ""){
    $barrel = 0;
}


$conn = new mysqli("localhost", "root", "123", "commission");
if ($conn->connect_error) {
    die("服务器连接失败: " . $conn->connect_error);
}
$result = $conn->query("SELECT sum(locks) as suml, sum(stocks) as sums, sum(barrels) as sumb FROM salesinformation WHERE id= '" .$id. "' and flag=0");

$row = mysqli_fetch_array($result);

$suml=$row['suml'];
$sums=$row['sums'];
$sumb=$row['sumb'];

if (($suml+$lock)>70){
    echo "<script>alert('加上此次的销售数据,您本月lock的总销售量已经超过单月最大允许量，请核对后重新输入');history.back();</script>";
    $conn->close();
}
else if (($sums+$stock)>80){
    echo "<script>alert('加上此次的销售数据,您本月stock的总销售量已经超过单月最大允许量，请核对后重新输入');history.back();</script>";
    $conn->close();
}
else if (($sumb+$barrel)>90){
    echo "<script>alert('加上此次的销售数据,您本月barrel的总销售量已经超过单月最大允许量，请核对后重新输入');history.back();</script>";
    $conn->close();
}
else if ($lock == -1){
    if ($result->num_rows == 0){
        echo "<script>alert('生成报告失败!您本月发送的所有数据早已生成过报告，请点击“获取业绩报告”进行查询');history.back();</script>";
    }
    else{
        $sale = $suml * 45 + $sums * 30 + $sumb * 25;
        if ($suml == 0 or $sumb == 0 or $sums == 0){
            $commission = 0;
        }
        else if($sale > 1800){
            $commission = ($sale - 1800) * 0.2 + 800 * 0.15 + 1000 * 0.1;
        }
        else if($sale > 1000 and $sale <= 1800){
            $commission = ($sale - 1000) * 0.15 + 1000 * 0.1;
        }
        else{
            $commission = $sale * 0.1;
        }
        $sql = "INSERT INTO report (id, suml, sums, sumb, amount, commission, date) VALUES ('" .$id. "' ,'" .$suml. "' , '" .$sums. "' , '".$sumb."', '" .$sale. "', '" .$commission. "', '" .$date. "')";
        if ($conn->query($sql) === TRUE) {
            $update = $conn->query("UPDATE salesinformation SET flag=1 WHERE id= '" .$id. "' and flag=0");
            if ($update === TRUE){
                echo "<script>alert('您本月的业绩报告已经成功生成，请点击“获取业绩报告”进行查询');history.back();</script>";
            }
            else{
                $delete = "DELETE from report WHERE id='" .$id. "' and date='".$date."'";
                echo "<script>alert('业绩报告生成失败,请重新尝试');history.back();</script>";
            }
        }
        else {
            echo "<script>alert('业绩报告生成失败,请重新尝试');history.back();</script>";
        }
        $conn->close();
    }
}
else{
    $sql = "INSERT INTO salesinformation (id, name, date, city, locks, stocks, barrels) VALUES ('" .$id. "','" .$name. "','" .$date. "','" .$city. "', '" .$lock. "', '" .$stock. "', '".$barrel."')";
    if ($conn->query($sql) === TRUE) {
        echo "<script>alert('电报发送成功');history.back();</script>";
    } else {
        echo "<script>alert('电报发送失败，请核对后重新发送');history.back();</script>";
    }

    $conn->close();
}

?>